<template>
  <div class="list_container">
    <div class="list_header">
      <div class="add_container">
        <div>
          店铺名称：
          <span><el-input v-model="name" placeholder="请输入内容"></el-input></span>
        </div>
        <div>
          店铺地址：<el-input v-model="add" placeholder="请输入内容"></el-input>
        </div>
        <div>
          店铺介绍：<el-input v-model="des" placeholder="请输入内容"></el-input>
        </div>
        <div>
            <el-button @click="onAdd()">添加</el-button>
        </div>
      </div>
      <div class="search_container">
        <el-input v-model="input" placeholder="请输店铺名称"></el-input>
        <el-button @click="onSearch()">搜索</el-button>
      </div>
    </div>
    <el-table
      :data="tableData.slice((currpage - 1) * pagesize,currpage*pagesize)"
      style="width: 100%"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品名称">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="所属店铺">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="店铺 ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="商品分类">
              <span>{{ props.row.category }}</span>
            </el-form-item>
            <el-form-item label="店铺地址">
              <span>{{ props.row.address }}</span>
            </el-form-item>
            <el-form-item label="商品描述">
              <span>{{ props.row.promotion_info }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" prop="name"></el-table-column>
      <el-table-column label="店铺地址" prop="address"></el-table-column>
      <el-table-column label="店铺介绍" prop="category"></el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="deleteRow(scope.$index, tableData)"
            type="text"
            size="small"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="tableData.length"
      :page-size="pagesize"
      @size-change="handleSize"
      @current-change="handleCurrent"
    ></el-pagination>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      list: [],
      tableData: [],
      pagesize: 5,
      currpage: 1,
      input: "",
      name:"",
      add:"",
      des:""
    };
  },
  methods: {
    handleSize(val) {
      this.pagesize = val;
    },
    handleCurrent(val) {
      this.currpage = val;
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    onAdd(){
        this.tableData.unshift({
            name:this.name,
            address:this.add,
            category:this.des
        })
    },
    onSearch(){
        var temp = []
        this.tableData.forEach((ele)=>{
            if(ele.name.includes(this.input)){
                temp.push(ele)
            }
        })
        this.tableData = temp
    }
  },
  mounted() {
    axios
      .get("http://localhost:8080/shopList.json")
      .then((res) => {
        console.log(res.data);
        this.tableData = res.data;
      })
      .catch((error) => {
        console.log(error);
      });
  },
};
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.list_header {
  width: 100%;
  height: 100px;
}
.add_container {
  width: 100%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}
.add_container div {
    width: 60%;
}
.search_container {
    width: 20%;
    margin-top: 10px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
}
</style>